package com.example.audiostreaming.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.audiostreaming.R;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import static com.example.audiostreaming.base.BaseApplication.getAppContext;

public class AdapterTrackRv extends RecyclerView.Adapter<AdapterTrackRv.InnerHolder> {

    private List<Track> mDetailAlbumListData = new ArrayList<>();//详情专辑列表数据
    //格式化时间
    private SimpleDateFormat mUpdateDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat mContentTimeFormat = new SimpleDateFormat("mm:ss");
    private OnDetailAlbumItemClickListener mOnDetailAlbumItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener=null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View detailAlbumItemView = LayoutInflater.from(getAppContext()).inflate(R.layout.activity_detail_item_rv, parent, false);
        return new InnerHolder(detailAlbumItemView);
    }


    @Override
    public void onBindViewHolder(@NonNull final InnerHolder holder, final int position) {
        holder.itemView.setTag(position);//holder内部持有者中的itemView条目视图设置标签为position当前位置
        holder.setUIData(mDetailAlbumListData.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(v.getContext(),"你点击了第"+holder.itemView.getTag()+"条目",Toast.LENGTH_SHORT).show();
                if (mOnDetailAlbumItemClickListener != null) {
                    mOnDetailAlbumItemClickListener.onDetailAlbumItemClick(mDetailAlbumListData,position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemLongClickListener != null) {
                    mOnItemLongClickListener.onItemLongClick(mDetailAlbumListData.get(position));
                }
                return true;//true,消费掉该事件
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mDetailAlbumListData != null) {
            return mDetailAlbumListData.size();
        }
        return 0;
    }

    public void setData(List<Track> detailAlbumTracks) {
        if (mDetailAlbumListData != null) {
            mDetailAlbumListData.clear();
            mDetailAlbumListData.addAll(detailAlbumTracks);
        }
        notifyDataSetChanged();
    }

    public void setOnDetailAlbumItemClickListener(OnDetailAlbumItemClickListener onDetailAlbumItemClickListener){
        mOnDetailAlbumItemClickListener = onDetailAlbumItemClickListener;
    }

    public interface OnDetailAlbumItemClickListener{
        void onDetailAlbumItemClick(List<Track> detailAlbumListData, int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener){
        this.mOnItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemLongClickListener{
        void onItemLongClick(Track track);
    }





    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setUIData(Track track) {
            TextView detailItemNumber = itemView.findViewById(R.id.number);
            TextView detailItemTitle = itemView.findViewById(R.id.title);
            TextView detailItemPlayCount = itemView.findViewById(R.id.play_count);
            TextView detailItemContentTime = itemView.findViewById(R.id.content_time);
            TextView detailItemUpdateData = itemView.findViewById(R.id.updateDate);


            if (detailItemNumber != null) {
                int itemNumber = (int) itemView.getTag()+1;
                detailItemNumber.setText(itemNumber+"");
            }
            if (detailItemTitle != null) {
                detailItemTitle.setText(track.getTrackTitle());
            }
            if (detailItemPlayCount != null) {
                if (track.getPlayCount()<10000) {
                    detailItemPlayCount.setText(track.getPlayCount()+"");
                }else if (track.getPlayCount()<100000000) {
                    detailItemPlayCount.setText(track.getPlayCount()/10000+"万");
                }else {
                    detailItemPlayCount.setText(track.getPlayCount()/100000000+"亿");
                }
            }
            if (detailItemContentTime != null) {
                detailItemContentTime.setText(mContentTimeFormat.format(track.getDuration()*1000));
            }
            if (detailItemUpdateData != null) {
                detailItemUpdateData.setText(mUpdateDateFormat.format(track.getUpdatedAt()));
            }
        }
    }
}
